//
//  JKNSDate.m
//  JKDateFormatter
//
//  Created by 王冲 on 2018/11/1.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "JKNSDate.h"

@implementation JKNSDate


+(NSString *)getDate:(NSString *)timestamp
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]];
    
    NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init];
    [formatter_ setDateFormat:@"YYYY年MM月"];
    
    NSString *dateString = [formatter_  stringFromDate:date];
    
    return dateString;
}

-(void)jkTimestampFromString:(NSString *)timeStr
format:(NSString *)format withJKTimestampBlock:(JKTimestampBlock)block
{
    
    timeStr = [timeStr stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
    timeStr = [timeStr stringByReplacingOccurrencesOfString:@"月" withString:@""];
    // 1.把你的 2018年10月先转化为 NSDate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSDate *date = [dateFormatter dateFromString:timeStr];
    
    NSTimeInterval number = [date timeIntervalSince1970];
    
    NSString *startTimestamp = [NSString stringWithFormat:@"%.f",number];
    NSString *endTimestamp = [self jkgetMonthBeginAndEndWith:date withType:@"2"];
    block(startTimestamp,endTimestamp);
}


// 1541001599: 2018/10/31 23:59:59
// 1541001600: 2018/11/01 00:00:00
// 1返回y月初1号的时间戳， 2 返回月末24：的时间戳
-(NSString *)jkgetMonthBeginAndEndWith:(NSDate *)newDate withType:(NSString *)type{
    
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM"];
    
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar setFirstWeekday:2];//设定周一为周首日
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:newDate];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
    }else {
        return @"";
    }
    
    // beginDate : 月初
    // endDate: 月末
    
    if ([type isEqualToString:@"1"]) {
        
        return [self nextDate:beginDate withType:@"1"];
        
    }else if([type isEqualToString:@"2"]){
        
        return [self nextDate:endDate withType:@"2"];
    }
    
    return @"";
}

// iOS 获取今天的0点时间和下一天的0点时间
-(NSString *)nextDate:(NSDate *)nowDate withType:(NSString *)type{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:nowDate];
    NSDate *startDate = [calendar dateFromComponents:components];
    NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];
    
    if ([type isEqualToString:@"1"]) {
        // 今天的时间戳
        return [NSString stringWithFormat:@"%ld",(long)[startDate timeIntervalSince1970]];
        
    }else if([type isEqualToString:@"2"]){
        // 下一天的时间戳
        return [NSString stringWithFormat:@"%ld",(long)[endDate timeIntervalSince1970]];
    }
    
    return @"";
}


@end
